//
//  CCPublishView.m
//  ccZone
//
//  Created by 陈小娟 on 2017/12/14.
//  Copyright © 2017年 CC. All rights reserved.
//

#define composeBtnW 80
#define composeBtnH 90

#import "CCPublishView.h"
#import "UIButton+VerticalImage.h"

@interface CCPublishView()

@property(nonatomic,strong)UIImageView *imageViewBg;
@property(nonatomic,strong)NSMutableArray<UIButton *>  *btnArray;
@property(nonatomic,strong)UIButton  *btnCancel;

@end

@implementation CCPublishView

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.imageViewBg];
        [self addSubview:self.btnCancel];
        [self addChildButton];
    }
    return self;
}

#pragma mark -- 布置button，点击动作
- (void)addChildButton{
    CGFloat itemW = composeBtnW;
    CGFloat itemH = composeBtnH;
    CGFloat originY = PORTRAIT_MAIN_SCREEN_H - itemH - (isIPhoneX ?34:0) - 50 -20;//20 是相对btncancel 的距离
    CGFloat itemMargin = (PORTRAIT_MAIN_SCREEN_W - 2 * composeBtnW)*0.33;
    for (NSInteger index = 0; index < 2; index++) {
        UIButton *btn = [[UIButton alloc] init];
        CGFloat x = itemMargin + index * itemW + index * itemMargin;
        CGRect rect = CGRectMake(x, originY , itemW, itemH);
        btn.frame = rect;
        if (index == 0) {
            btn.tag = 100;
            [btn setImage:kTYIMAGE(@"rs_compose_post") forState:UIControlStateNormal];
            [btn setTitle:@"帖子" forState:UIControlStateNormal];
        }else{
            btn.tag = 200;
            [btn setImage:kTYIMAGE(@"rs_compose_active")
                 forState:UIControlStateNormal];
            [btn setTitle:@"动态" forState:UIControlStateNormal];
        }
        btn.titleLabel.font = [UIFont systemFontOfSize:16];
        [btn setTitleColor:TY_RGB(34,28,28, 0)
                  forState:UIControlStateNormal];
        [btn setTitleColor:TY_RGB(34,28,28, 0)
                  forState:UIControlStateHighlighted];
        
        [btn addTarget:self action:@selector(actionClick:) forControlEvents:1<<6];
        [btn setImagePositionWithType:RSImagePositionTypeTop spacing:6.f];
        [self.btnArray addObject:btn];
        [self addSubview:btn];
    }
}
#pragma mark -- 获取动作，由代理处理
-(void)actionClick:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(ccPublishView:select:)]) {
        [self.delegate ccPublishView:self select:sender.tag];
    }
    [self hidden];
}
// 按钮滑出动画
- (void)touchesBegan:(NSSet<UITouch *> *)touches
           withEvent:(UIEvent *)event {
    [self hidden];
}
#pragma mark -- 消失
-(void)hidden {
    [UIView animateWithDuration:0.3 animations:^{
        self.btnCancel.alpha = 0.1;
    }];
    [self removeFromSuperview];
}
#pragma mark -- 显示
- (void)showWithController:(UIViewController *)fromController {
    [fromController.view addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        self.btnCancel.alpha = 1.0;
    }];
}

- (UIButton *)btnCancel{
    if (!_btnCancel) {
        CGFloat height = 50;
        CGFloat width = 50;
        CGRect rect = CGRectMake(PORTRAIT_MAIN_SCREEN_W *0.5 -width * 0.5, PORTRAIT_MAIN_SCREEN_H - height -(isIPhoneX?34:0), width, height);
        _btnCancel = [[UIButton alloc] init];
        _btnCancel.adjustsImageWhenDisabled = NO;
        _btnCancel.alpha = 0.1;
        _btnCancel.adjustsImageWhenHighlighted = NO;
        [_btnCancel setBackgroundImage:kTYIMAGE(@"icon_camera_fill") forState:UIControlStateNormal];
        [_btnCancel setBackgroundImage:kTYIMAGE(@"icon_camera_fill")
                              forState:UIControlStateSelected];
        [_btnCancel addTarget:self action:@selector(actionBtn:) forControlEvents:1<<6];
        _btnCancel.frame = rect;
    }
    return _btnCancel;
}

- (UIImageView *)imageViewBg{
    if (!_imageViewBg) {
        _imageViewBg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, PORTRAIT_MAIN_SCREEN_W, PORTRAIT_MAIN_SCREEN_H)];
        _imageViewBg.image = kTYIMAGE(@"rs_compose_bg");
    }
    return _imageViewBg;
}

-(void)actionBtn:(id)sender {
    [self hidden];
}

@end
